<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>人生重开模拟器</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .game-container {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.3);
            max-width: 800px;
            width: 100%;
            text-align: center;
        }
        
        h1 {
            color: #333;
            margin-bottom: 20px;
            font-size: 2.5em;
        }
        
        .game-screen {
            display: none;
            text-align: left;
        }
        
        .game-screen.active {
            display: block;
        }
        
        .character-creation {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
        }
        
        .attribute-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 10px 0;
            padding: 10px;
            background: white;
            border-radius: 8px;
        }
        
        .attribute-name {
            font-weight: bold;
            color: #333;
            min-width: 80px;
        }
        
        .attribute-controls {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .attribute-value {
            font-weight: bold;
            color: #007bff;
            min-width: 30px;
            text-align: center;
        }
        
        .points-remaining {
            background: #007bff;
            color: white;
            padding: 10px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-weight: bold;
        }
        
        .life-events {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            max-height: 400px;
            overflow-y: auto;
        }
        
        .event {
            margin: 10px 0;
            padding: 10px;
            background: white;
            border-radius: 8px;
            border-left: 4px solid #007bff;
        }
        
        .event.age {
            border-left-color: #28a745;
        }
        
        .event.good {
            border-left-color: #28a745;
        }
        
        .event.bad {
            border-left-color: #dc3545;
        }
        
        .event.neutral {
            border-left-color: #ffc107;
        }
        
        .event.special {
            border-left-color: #6f42c1;
            background: linear-gradient(90deg, #f8f9ff 0%, #e6d7ff 100%);
            font-weight: bold;
            color: #4a148c;
            box-shadow: 0 2px 8px rgba(111, 66, 193, 0.2);
        }
        
        .character-stats {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
        }
        
        .stat-row {
            display: flex;
            justify-content: space-between;
            margin: 5px 0;
            padding: 5px 0;
            border-bottom: 1px solid #dee2e6;
        }
        
        .game-buttons {
            display: flex;
            flex-direction: column;
            gap: 10px;
            margin-top: 20px;
        }
        
        button {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .btn-primary {
            background: #007bff;
            color: white;
        }
        
        .btn-primary:hover {
            background: #0056b3;
            transform: translateY(-2px);
        }
        
        .btn-success {
            background: #28a745;
            color: white;
        }
        
        .btn-success:hover {
            background: #1e7e34;
            transform: translateY(-2px);
        }
        
        .btn-warning {
            background: #ffc107;
            color: #212529;
        }
        
        .btn-warning:hover {
            background: #e0a800;
            transform: translateY(-2px);
        }
        
        .btn-danger {
            background: #dc3545;
            color: white;
        }
        
        .btn-danger:hover {
            background: #c82333;
            transform: translateY(-2px);
        }
        
        .ending-screen {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
        }
        
        .ending-title {
            font-size: 24px;
            font-weight: bold;
            color: #333;
            margin-bottom: 15px;
        }
        
        .ending-description {
            font-size: 16px;
            line-height: 1.6;
            color: #666;
            margin-bottom: 15px;
        }
        
        .life-summary {
            background: white;
            border-radius: 8px;
            padding: 15px;
            margin-top: 15px;
        }
        
        .progress-bar {
            width: 100%;
            height: 20px;
            background: #e9ecef;
            border-radius: 10px;
            overflow: hidden;
            margin: 5px 0;
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #007bff, #0056b3);
            transition: width 0.3s ease;
        }
        
        @media (max-width: 768px) {
            .game-container {
                padding: 15px;
            }
            
            h1 {
                font-size: 2em;
            }
            
            .attribute-row {
                flex-direction: column;
                gap: 5px;
            }
            
            .attribute-controls {
                width: 100%;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="game-container">
        <h1>🎭 人生重开模拟器</h1>
        
        <!-- 开始界面 -->
        <div id="startScreen" class="game-screen active">
            <div class="character-creation">
                <h2>🎯 开始新的人生</h2>
                <p>分配你的初始属性点，开启全新的人生旅程！</p>
                
                <div class="points-remaining">
                    剩余属性点: <span id="remainingPoints">20</span>
                </div>
                
                <div class="attribute-row">
                    <div class="attribute-name">颜值</div>
                    <div class="attribute-controls">
                        <button onclick="adjustAttribute('appearance', -1)">-</button>
                        <div class="attribute-value" id="appearance">5</div>
                        <button onclick="adjustAttribute('appearance', 1)">+</button>
                    </div>
                </div>
                
                <div class="attribute-row">
                    <div class="attribute-name">智力</div>
                    <div class="attribute-controls">
                        <button onclick="adjustAttribute('intelligence', -1)">-</button>
                        <div class="attribute-value" id="intelligence">5</div>
                        <button onclick="adjustAttribute('intelligence', 1)">+</button>
                    </div>
                </div>
                
                <div class="attribute-row">
                    <div class="attribute-name">体质</div>
                    <div class="attribute-controls">
                        <button onclick="adjustAttribute('strength', -1)">-</button>
                        <div class="attribute-value" id="strength">5</div>
                        <button onclick="adjustAttribute('strength', 1)">+</button>
                    </div>
                </div>
                
                <div class="attribute-row">
                    <div class="attribute-name">家境</div>
                    <div class="attribute-controls">
                        <button onclick="adjustAttribute('family', -1)">-</button>
                        <div class="attribute-value" id="family">5</div>
                        <button onclick="adjustAttribute('family', 1)">+</button>
                    </div>
                </div>
                
                <div class="game-buttons">
                    <button class="btn-primary" onclick="startLife()">开始人生</button>
                    <button class="btn-warning" onclick="randomAttributes()">随机属性</button>
                </div>
            </div>
        </div>
        
        <!-- 人生进行界面 -->
        <div id="lifeScreen" class="game-screen">
            <div class="character-stats">
                <h3>📊 角色状态</h3>
                <div class="stat-row">
                    <span>年龄:</span>
                    <span id="currentAge">0</span>
                </div>
                <div class="stat-row">
                    <span>颜值:</span>
                    <span id="currentAppearance">5</span>
                </div>
                <div class="stat-row">
                    <span>智力:</span>
                    <span id="currentIntelligence">5</span>
                </div>
                <div class="stat-row">
                    <span>体质:</span>
                    <span id="currentStrength">5</span>
                </div>
                <div class="stat-row">
                    <span>家境:</span>
                    <span id="currentFamily">5</span>
                </div>
                <div class="stat-row">
                    <span>快乐:</span>
                    <span id="currentHappiness">50</span>
                </div>
                <div class="stat-row">
                    <span>健康:</span>
                    <span id="currentHealth">100</span>
                </div>
            </div>
            
            <div class="life-events" id="lifeEvents">
                <!-- 人生事件将在这里显示 -->
            </div>
            
            <div class="game-buttons">
                <button class="btn-success" onclick="nextYear()">下一年</button>
                <button class="btn-warning" onclick="autoLife()">自动人生</button>
                <button class="btn-danger" onclick="endLife()">结束人生</button>
            </div>
        </div>
        
        <!-- 结局界面 -->
        <div id="endingScreen" class="game-screen">
            <div class="ending-screen">
                <div class="ending-title" id="endingTitle">人生结束</div>
                <div class="ending-description" id="endingDescription">
                    你的人生已经结束，回顾你的一生...
                </div>
                
                <div class="life-summary">
                    <h4>📈 人生总结</h4>
                    <div class="stat-row">
                        <span>享年:</span>
                        <span id="finalAge">0</span>
                    </div>
                    <div class="stat-row">
                        <span>最终颜值:</span>
                        <span id="finalAppearance">0</span>
                    </div>
                    <div class="stat-row">
                        <span>最终智力:</span>
                        <span id="finalIntelligence">0</span>
                    </div>
                    <div class="stat-row">
                        <span>最终体质:</span>
                        <span id="finalStrength">0</span>
                    </div>
                    <div class="stat-row">
                        <span>最终家境:</span>
                        <span id="finalFamily">0</span>
                    </div>
                    <div class="stat-row">
                        <span>人生评价:</span>
                        <span id="lifeRating">普通</span>
                    </div>
                </div>
                
                <div class="game-buttons">
                    <button class="btn-primary" onclick="restartGame()">重新开始</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 游戏状态
        let gameState = {
            attributes: {
                appearance: 5,
                intelligence: 5,
                strength: 5,
                family: 5
            },
            currentStats: {
                age: 0,
                appearance: 5,
                intelligence: 5,
                strength: 5,
                family: 5,
                happiness: 50,
                health: 100
            },
            remainingPoints: 20,
            isAlive: true,
            autoPlay: false,
            events: [],
            // 特殊物品和成就
            specialItems: [],
            achievements: [],
            cultivationPath: null, // 修真路径：null, 'xiuzhen', 'hunxiu'
            cultivationLevel: 0
        };
        
        // 人生事件库
        const lifeEvents = {
            childhood: [
                { age: 0, text: "你出生了，是一个健康的宝宝！", type: "age", effect: {} },
                { age: 1, text: "你学会了走路，父母很开心。", type: "good", effect: { happiness: 5 } },
                { age: 3, text: "你开始上幼儿园，认识了很多小伙伴。", type: "good", effect: { happiness: 10 } },
                { age: 5, text: "你表现出了对绘画的天赋。", type: "good", effect: { intelligence: 1 } },
                { age: 6, text: "你上小学了，学习很认真。", type: "neutral", effect: { intelligence: 2 } },
                { age: 8, text: "你在学校被欺负了，很难过。", type: "bad", effect: { happiness: -10 } },
                { age: 10, text: "你参加了数学竞赛，获得了好成绩。", type: "good", effect: { intelligence: 3, happiness: 15 } }
            ],
            youth: [
                { age: 12, text: "你进入了初中，开始了新的学习生活。", type: "neutral", effect: {} },
                { age: 14, text: "你青春期到了，长高了。", type: "good", effect: { appearance: 2, strength: 1 } },
                { age: 16, text: "你谈了第一次恋爱，很甜蜜。", type: "good", effect: { happiness: 20 } },
                { age: 18, text: "你参加了高考，成绩不错。", type: "good", effect: { intelligence: 5 } },
                { age: 18, text: "你考上了理想的大学！", type: "good", effect: { happiness: 30, intelligence: 3 } }
            ],
            adult: [
                { age: 22, text: "你大学毕业了，找到了工作。", type: "good", effect: { family: 10, happiness: 25 } },
                { age: 25, text: "你在工作中表现出色，得到了晋升。", type: "good", effect: { family: 20, happiness: 20 } },
                { age: 28, text: "你结婚了，开始了新的生活。", type: "good", effect: { happiness: 40 } },
                { age: 30, text: "你有了一个可爱的孩子。", type: "good", effect: { happiness: 50 } },
                { age: 35, text: "你创业成功了，成为了老板。", type: "good", effect: { family: 50, happiness: 30 } },
                { age: 40, text: "你开始注重养生，身体更健康了。", type: "good", effect: { health: 20, strength: 2 } }
            ],
            middle: [
                { age: 45, text: "你开始有中年危机，感到焦虑。", type: "bad", effect: { happiness: -15 } },
                { age: 50, text: "你的孩子长大了，很懂事。", type: "good", effect: { happiness: 25 } },
                { age: 55, text: "你开始退休生活，很悠闲。", type: "neutral", effect: { happiness: 10 } },
                { age: 60, text: "你开始享受天伦之乐。", type: "good", effect: { happiness: 30 } }
            ],
            old: [
                { age: 65, text: "你的身体开始衰老。", type: "bad", effect: { health: -20, strength: -3 } },
                { age: 70, text: "你有了孙子，很开心。", type: "good", effect: { happiness: 40 } },
                { age: 75, text: "你的健康状况开始恶化。", type: "bad", effect: { health: -30 } },
                { age: 80, text: "你回顾自己的一生，感到很满足。", type: "good", effect: { happiness: 50 } }
            ]
        };
        
        // 随机事件
        const randomEvents = [
            { text: "你中了一张彩票！", type: "good", effect: { family: 30, happiness: 25 }, minAge: 18 },
            { text: "你生了一场大病。", type: "bad", effect: { health: -25, happiness: -15 }, minAge: 0 },
            { text: "你遇到了贵人相助。", type: "good", effect: { family: 20, happiness: 15 }, minAge: 16 },
            { text: "你遭遇了车祸。", type: "bad", effect: { health: -35, happiness: -20 }, minAge: 5 },
            { text: "你学会了新的技能。", type: "good", effect: { intelligence: 5, happiness: 10 }, minAge: 6 },
            { text: "你投资失败了。", type: "bad", effect: { family: -30, happiness: -25 }, minAge: 22 },
            { text: "你参加了一个有趣的聚会。", type: "good", effect: { happiness: 20 }, minAge: 15 },
            { text: "你失去了工作。", type: "bad", effect: { family: -20, happiness: -15 }, minAge: 22 },
            { text: "你遇到了真爱。", type: "good", effect: { happiness: 40 }, minAge: 16 },
            { text: "你被朋友背叛了。", type: "bad", effect: { happiness: -25 }, minAge: 10 },
            { text: "你发现了一个神秘的小盒子，无法打开。", type: "special", effect: {}, minAge: 20, specialItem: "mysteryBox" },
            { text: "你获得了一本古籍《元神不灭》。", type: "special", effect: { intelligence: 10 }, minAge: 30, specialItem: "ancientBook" },
            { text: "你体验了一次奇异的轮回感。", type: "special", effect: { happiness: 15 }, minAge: 40, specialItem: "reincarnation" },
            { text: "你遇到了一位神秘的修行者。", type: "special", effect: { intelligence: 5, happiness: 10 }, minAge: 25, specialItem: "mysteriousCultivator" }
        ];
        
        // 调整属性
        function adjustAttribute(attr, change) {
            if (gameState.remainingPoints <= 0 && change > 0) return;
            if (gameState.attributes[attr] <= 0 && change < 0) return;
            if (gameState.attributes[attr] >= 10 && change > 0) return;
            
            gameState.attributes[attr] += change;
            gameState.remainingPoints -= change;
            
            document.getElementById(attr).textContent = gameState.attributes[attr];
            document.getElementById('remainingPoints').textContent = gameState.remainingPoints;
        }
        
        // 随机属性
        function randomAttributes() {
            const attributes = ['appearance', 'intelligence', 'strength', 'family'];
            gameState.remainingPoints = 20;
            
            attributes.forEach(attr => {
                gameState.attributes[attr] = 0;
            });
            
            while (gameState.remainingPoints > 0) {
                const randomAttr = attributes[Math.floor(Math.random() * attributes.length)];
                if (gameState.attributes[randomAttr] < 10) {
                    gameState.attributes[randomAttr]++;
                    gameState.remainingPoints--;
                }
            }
            
            attributes.forEach(attr => {
                document.getElementById(attr).textContent = gameState.attributes[attr];
            });
            
            document.getElementById('remainingPoints').textContent = gameState.remainingPoints;
        }
        
        // 开始人生
        function startLife() {
            if (gameState.remainingPoints > 0) {
                alert('请分配完所有属性点！');
                return;
            }
            
            // 初始化当前状态
            gameState.currentStats = {
                age: 0,
                appearance: gameState.attributes.appearance,
                intelligence: gameState.attributes.intelligence,
                strength: gameState.attributes.strength,
                family: gameState.attributes.family,
                happiness: 50,
                health: 100
            };
            
            gameState.isAlive = true;
            gameState.events = [];
            
            // 切换界面
            document.getElementById('startScreen').classList.remove('active');
            document.getElementById('lifeScreen').classList.add('active');
            
            updateStats();
            addEvent("你出生了，开始了新的人生！", "age");
        }
        
        // 更新状态显示
        function updateStats() {
            document.getElementById('currentAge').textContent = gameState.currentStats.age;
            document.getElementById('currentAppearance').textContent = gameState.currentStats.appearance;
            document.getElementById('currentIntelligence').textContent = gameState.currentStats.intelligence;
            document.getElementById('currentStrength').textContent = gameState.currentStats.strength;
            document.getElementById('currentFamily').textContent = gameState.currentStats.family;
            document.getElementById('currentHappiness').textContent = gameState.currentStats.happiness;
            document.getElementById('currentHealth').textContent = gameState.currentStats.health;
        }
        
        // 添加事件
        function addEvent(text, type) {
            const eventDiv = document.createElement('div');
            eventDiv.className = `event ${type}`;
            eventDiv.textContent = `[${gameState.currentStats.age}岁] ${text}`;
            
            const eventsContainer = document.getElementById('lifeEvents');
            eventsContainer.appendChild(eventDiv);
            eventsContainer.scrollTop = eventsContainer.scrollHeight;
            
            gameState.events.push({ age: gameState.currentStats.age, text, type });
        }
        
        // 下一年
        function nextYear() {
            if (!gameState.isAlive) return;
            
            gameState.currentStats.age++;
            
            // 年龄增长效果
            if (gameState.currentStats.age > 50) {
                gameState.currentStats.strength = Math.max(0, gameState.currentStats.strength - 1);
                gameState.currentStats.health = Math.max(0, gameState.currentStats.health - 5);
            }
            
            if (gameState.currentStats.age > 70) {
                gameState.currentStats.appearance = Math.max(0, gameState.currentStats.appearance - 1);
                gameState.currentStats.health = Math.max(0, gameState.currentStats.health - 10);
            }
            
            // 触发年龄事件
            triggerAgeEvents();
            
            // 随机事件
            if (Math.random() < 0.15) {
                triggerRandomEvent();
            }
            
            // 检查特殊规则
        function checkSpecialRules() {
            const age = gameState.currentStats.age;
            
            // 检查修真线：拥有神秘的小盒子且活到100岁
            if (gameState.specialItems.includes("mysteryBox") && age >= 100 && gameState.cultivationPath === null) {
                gameState.cultivationPath = "xiuzhen";
                gameState.cultivationLevel = 1;
                addEvent("你打开了神秘的小盒子，里面是修真秘籍！你开启了修真之路！", "special");
                addEvent("你的身体发生了奇妙的变化，寿命得到了极大的延长！", "special");
                gameState.currentStats.health = 150;
                gameState.currentStats.strength += 20;
                gameState.achievements.push("修真者");
            }
            
            // 检查魂修线：有《元神不灭》且体验过轮回
            if (gameState.specialItems.includes("ancientBook") && gameState.specialItems.includes("reincarnation") && gameState.cultivationPath === null) {
                gameState.cultivationPath = "hunxiu";
                gameState.cultivationLevel = 1;
                addEvent("《元神不灭》与你的轮回感产生了共鸣！你开启了魂修之路！", "special");
                addEvent("你的元神得到了觉醒，开始修炼魂道！", "special");
                gameState.currentStats.intelligence += 30;
                gameState.currentStats.happiness += 50;
                gameState.achievements.push("魂修者");
            }
            
            // 修真者等级提升
            if (gameState.cultivationPath === "xiuzhen" && age > 100) {
                const newLevel = Math.floor((age - 100) / 20) + 1;
                if (newLevel > gameState.cultivationLevel) {
                    gameState.cultivationLevel = newLevel;
                    addEvent(`你的修为提升到了第${newLevel}层！`, "special");
                    gameState.currentStats.strength += 10;
                    gameState.currentStats.health += 20;
                }
            }
            
            // 魂修者等级提升
            if (gameState.cultivationPath === "hunxiu" && age > 50) {
                const newLevel = Math.floor((age - 50) / 15) + 1;
                if (newLevel > gameState.cultivationLevel) {
                    gameState.cultivationLevel = newLevel;
                    addEvent(`你的魂力提升到了第${newLevel}层！`, "special");
                    gameState.currentStats.intelligence += 15;
                    gameState.currentStats.happiness += 20;
                }
            }
        }
        
        // 检查生存状态
            checkSurvival();
            
            updateStats();
        }
        
        // 触发年龄事件
        function triggerAgeEvents() {
            const age = gameState.currentStats.age;
            let eventPool = [];
            
            if (age <= 11) eventPool = lifeEvents.childhood;
            else if (age <= 18) eventPool = lifeEvents.youth;
            else if (age <= 44) eventPool = lifeEvents.adult;
            else if (age <= 64) eventPool = lifeEvents.middle;
            else eventPool = lifeEvents.old;
            
            const ageEvents = eventPool.filter(event => event.age === age);
            
            ageEvents.forEach(event => {
                addEvent(event.text, event.type);
                applyEffects(event.effect);
            });
        }
        
        // 触发随机事件
        function triggerRandomEvent() {
            const currentAge = gameState.currentStats.age;
            const availableEvents = randomEvents.filter(event => event.minAge <= currentAge);
            
            if (availableEvents.length > 0) {
                const event = availableEvents[Math.floor(Math.random() * availableEvents.length)];
                addEvent(event.text, event.type);
                applyEffects(event.effect);
                
                // 处理特殊物品
                if (event.specialItem && !gameState.specialItems.includes(event.specialItem)) {
                    gameState.specialItems.push(event.specialItem);
                    addEvent(`你获得了特殊物品：${event.specialItem}`, "special");
                }
                
                // 检查特殊规则
                checkSpecialRules();
            }
        }
        
        // 应用效果
        function applyEffects(effects) {
            Object.keys(effects).forEach(stat => {
                if (gameState.currentStats[stat] !== undefined) {
                    gameState.currentStats[stat] = Math.max(0, Math.min(100, gameState.currentStats[stat] + effects[stat]));
                }
            });
        }
        
        // 检查生存状态
        function checkSurvival() {
            if (gameState.currentStats.health <= 0) {
                gameState.isAlive = false;
                addEvent("你因为健康问题去世了。", "bad");
                endLife();
                return;
            }
            
            // 普通人的死亡概率
            let deathChance = 0;
            if (gameState.cultivationPath === null) {
                if (gameState.currentStats.age > 90) deathChance = 0.1;
                if (gameState.currentStats.age > 100) deathChance = 0.3;
                if (gameState.currentStats.age > 110) deathChance = 0.5;
            } 
            // 修真者的死亡概率（寿命更长）
            else if (gameState.cultivationPath === "xiuzhen") {
                if (gameState.currentStats.age > 150) deathChance = 0.05;
                if (gameState.currentStats.age > 200) deathChance = 0.1;
                if (gameState.currentStats.age > 250) deathChance = 0.2;
            }
            // 魂修者的死亡概率（元神不灭）
            else if (gameState.cultivationPath === "hunxiu") {
                if (gameState.currentStats.age > 120) deathChance = 0.03;
                if (gameState.currentStats.age > 150) deathChance = 0.08;
                if (gameState.currentStats.age > 180) deathChance = 0.15;
            }
            
            if (deathChance > 0 && Math.random() < deathChance) {
                gameState.isAlive = false;
                if (gameState.cultivationPath === "xiuzhen") {
                    addEvent("你的修为已达瓶颈，肉身寿元耗尽，元神飞升！", "special");
                } else if (gameState.cultivationPath === "hunxiu") {
                    addEvent("你的元神修炼大成，脱离轮回，永生不灭！", "special");
                } else {
                    addEvent("你安详地离开了人世。", "neutral");
                }
                endLife();
            }
        }
        
        // 自动人生
        function autoLife() {
            if (gameState.autoPlay) {
                gameState.autoPlay = false;
                return;
            }
            
            gameState.autoPlay = true;
            autoPlayLoop();
        }
        
        function autoPlayLoop() {
            if (!gameState.autoPlay || !gameState.isAlive) {
                gameState.autoPlay = false;
                return;
            }
            
            nextYear();
            setTimeout(autoPlayLoop, 1000);
        }
        
        // 结束人生
        function endLife() {
            gameState.isAlive = false;
            gameState.autoPlay = false;
            
            // 计算人生评价
            let totalStats = gameState.currentStats.appearance + 
                             gameState.currentStats.intelligence + 
                             gameState.currentStats.strength + 
                             gameState.currentStats.family + 
                             gameState.currentStats.happiness + 
                             gameState.currentStats.health;
            
            // 修真者和魂修者获得额外加成
            if (gameState.cultivationPath === "xiuzhen") {
                totalStats += gameState.cultivationLevel * 50;
            } else if (gameState.cultivationPath === "hunxiu") {
                totalStats += gameState.cultivationLevel * 60;
            }
            
            let rating = "普通";
            if (totalStats > 600) rating = "神话";
            else if (totalStats > 500) rating = "传奇";
            else if (totalStats > 400) rating = "优秀";
            else if (totalStats > 300) rating = "良好";
            else if (totalStats < 200) rating = "困难";
            else if (totalStats < 150) rating = "悲惨";
            
            // 显示结局
            document.getElementById('finalAge').textContent = gameState.currentStats.age;
            document.getElementById('finalAppearance').textContent = gameState.currentStats.appearance;
            document.getElementById('finalIntelligence').textContent = gameState.currentStats.intelligence;
            document.getElementById('finalStrength').textContent = gameState.currentStats.strength;
            document.getElementById('finalFamily').textContent = gameState.currentStats.family;
            document.getElementById('lifeRating').textContent = rating;
            
            // 生成结局描述
            let endingText = `你享年${gameState.currentStats.age}岁。`;
            
            if (gameState.cultivationPath === "xiuzhen") {
                endingText += `你成为了一名修真者，修为达到了第${gameState.cultivationLevel}层。`;
                if (gameState.cultivationLevel >= 5) {
                    endingText += "你最终破碎虚空，飞升成仙，成就了无上道果！";
                } else if (gameState.cultivationLevel >= 3) {
                    endingText += "你虽然未能飞升，但也成为了人间至强者，受人敬仰！";
                } else {
                    endingText += "你的修真之路虽然刚刚起步，但已经超越了凡人极限！";
                }
            } else if (gameState.cultivationPath === "hunxiu") {
                endingText += `你成为了一名魂修者，魂力达到了第${gameState.cultivationLevel}层。`;
                if (gameState.cultivationLevel >= 5) {
                    endingText += "你最终修炼成元神不灭之体，超脱轮回，永生不灭！";
                } else if (gameState.cultivationLevel >= 3) {
                    endingText += "你的元神已经十分强大，可以脱离肉身存在！";
                } else {
                    endingText += "你的魂修之路虽然刚开始，但已经感受到了元神的力量！";
                }
            } else {
                if (rating === "传奇") {
                    endingText += "你的人生堪称传奇，在各个方面都取得了卓越的成就！";
                } else if (rating === "优秀") {
                    endingText += "你的人生非常成功，拥有幸福美满的生活！";
                } else if (rating === "良好") {
                    endingText += "你的人生很不错，虽然有些遗憾但总体是幸福的。";
                } else if (rating === "困难") {
                    endingText += "你的人生充满了挑战，但你依然坚强地生活着。";
                } else {
                    endingText += "你的人生很艰难，经历了许多不幸的事情。";
                }
            }
            
            // 显示成就
            if (gameState.achievements.length > 0) {
                endingText += `\n成就：${gameState.achievements.join("、")}`;
            }
            
            document.getElementById('endingDescription').textContent = endingText;
            
            // 切换到结局界面
            document.getElementById('lifeScreen').classList.remove('active');
            document.getElementById('endingScreen').classList.add('active');
        }
        
        // 重新开始
        function restartGame() {
            // 重置游戏状态
            gameState = {
                attributes: {
                    appearance: 5,
                    intelligence: 5,
                    strength: 5,
                    family: 5
                },
                currentStats: {
                    age: 0,
                    appearance: 5,
                    intelligence: 5,
                    strength: 5,
                    family: 5,
                    happiness: 50,
                    health: 100
                },
                remainingPoints: 20,
                isAlive: true,
                autoPlay: false,
                events: [],
                // 重置特殊物品和成就
                specialItems: [],
                achievements: [],
                cultivationPath: null,
                cultivationLevel: 0
            };
            
            // 重置界面
            document.getElementById('lifeScreen').classList.remove('active');
            document.getElementById('endingScreen').classList.remove('active');
            document.getElementById('startScreen').classList.add('active');
            
            // 重置显示
            document.getElementById('appearance').textContent = 5;
            document.getElementById('intelligence').textContent = 5;
            document.getElementById('strength').textContent = 5;
            document.getElementById('family').textContent = 5;
            document.getElementById('remainingPoints').textContent = 20;
            document.getElementById('lifeEvents').innerHTML = '';
        }
    </script>
</body>
</html>